//插入数据前对数据进行格式化
function operateFormatter() {
    return "<button class='btn-xs btn-primary btn-outline' style='margin-right:5px;border: 1px solid rgb(77, 194, 181);' onclick='detailed(this)'>详情</button>" +
        "<button class='btn-xs btn-warning btn-outline' style='border: 1px solid rgb(250, 190, 124);' onclick='modify(this)'>修改</button>"
}

function meetStatusFormatter(vaule, row, index) {

    var meetStart = new Date(row.meetStart);
    var meetEnd = new Date(row.meetStart);
    var current = new Date();
    timeStamp = meetEnd.getTime();
    meetEnd.setTime(timeStamp + 1000 * row.meetDuring)

    if (current < meetStart) {
        return "<i style='color:#4CAF50;' class=''>未开始</i>"
    }
    if (meetEnd > current) {
        return "<i style='color:#d99600;'>正在召开</i>"
    }
    if (current > meetEnd) {
        return "<i style='color:#2196F3;'>会议结束</i>"
    }

}

(function () {
    $('#meetTable').bootstrapTable({
        url: "meetList",
        method: "post",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded",
        search: false,
        dataField: "data",
        paginationPreText: '上一页',
        paginationNextText: '下一页',
        height: $(window).height() - 50,
        pagination: true,
        showRefresh: true,
        showToggle: true,
        showColumns: true,
        pageNumber: 1,
        pageSize: 8,
        pageList: [8, 15, 25, 50],
        queryParamsType: '',
        iconSize: 'outline',
        toolbar: '#meetTableTools',
        sidePagination: "server", //服务端处理分页
        icons: {
            refresh: 'glyphicon-repeat',
            toggle: 'glyphicon-list-alt',
            columns: 'glyphicon-list'
        },
        queryParams: pageParams,//传递参数（*）
        responseHandler: responseHandler,
        columns: [
            {
                title: '全选',
                field: 'select',
                //复选框
                checkbox: true,

                align: 'center',
                valign: 'middle'
            },
            {
                title: '会议编号',
                field: 'meetId',
                visible: true
            },
            {
                title: '会议组织者',
                field: 'organizerId',
                sortable: false
            },
            {
                title: '会议地址',
                field: 'meetAddress',
                sortable: false
            },
            {
                title: '会议时间',
                field: 'meetStart',
                formatter: dateTimeFormat
            },
            {
                title: '会议时长',
                field: 'meetDuring'
            },
            {
                title: '会议状态',
                field: 'meetStatus',
                sortable: false,
                formatter: meetStatusFormatter
            },
            {
                title: '操作',
                field: 'operation',
                align: 'center',
                //列数据格式化，需要加单位等，自定义函数格式化，参数
                formatter: operateFormatter
            }
        ]
    });
    $(window).resize(function () {
        $('#meetTable').bootstrapTable('resetView', {
            height: tableHeight()
        });
    });

    //查询点击按钮
    $('#search_btn').click(function () {
        var searchInfo = $("[name='search_info']").val();
        console.log(searchInfo);
        if (searchInfo == undefined || searchInfo.length == 0) {
            layer.tips("输入搜索内容为空", "[name='search_info']", {
                tips: [1, '#3595CC'],
                time: 2000
            });
            return;
        }
        $('#meetTable').bootstrapTable('refresh', {url: 'meetList'});
    })

    //得到查询的参数
    function pageParams(params) {
        var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            pageSize: params.pageSize,  //页面大小
            pageNumber: params.pageNumber, //页码
            searchInfo: $("[name='search_info']").val()
        };
        return temp;
    };

//得到查询的参数
    function queryParams(params) {
        var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            pageSize: params.pageSize,  //页面大小
            pageNumber: params.pageNumber, //页码
            searchInfo: $("[name='search_info']").val()
        };
        return temp;
    };

    function responseHandler(result) {
        //在ajax获取到数据，渲染表格之前，修改数据源
        if (result.code == 500300 && $("[name='search_info']").val().length > 0) {
            layer.alert("查询结果为空", {
                icon: 0,
                skin: 'layui-layer-molv',
            })
            return {
                total: 0,
                data: null
            }
        }
        //如果没有错误则返回数据，渲染表格
        return {
            total: result.total,//总页数,前面的key必须为"total"
            data: result.data //行数据，前面的key要与之前设置的dataField的值一致.
        };
    }

    function tableHeight() {
        //可以根据自己页面情况进行调整
        return $(window).height() - 50;
    }

    function dateTimeFormat(value) {
        var dateVal = value + "";
        if (value != null) {
            var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

            return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
        }
    }
})();

